package com.example.web;


import com.example.fanqie.entity.Comment;
import com.example.fanqie.entity.User;
import com.example.fanqie.mapper.CommentMapper;
import com.example.fanqie.mapper.UserMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentDao {

    @Resource
    private CommentMapper   commentMapper;

    @Resource
    private UserMapper userMapper;


    //发表我的评论
    @RequestMapping("/submitComment")
    public int submitComment(@RequestBody Comment comment){
        Comment comment1 = commentMapper.queryMyComment(comment.getNovelId(), comment.getUserId(), comment.getReply());
       if (comment1 == null){
           int insert = commentMapper.insert(comment);
           return  insert;
       }
       else{
            commentMapper.updateById(comment);

           return 2;
       }



    }

    @RequestMapping("/queryMyComment")

    public Comment queryMyComment(@RequestBody Comment comment){
        Comment comment1 = commentMapper.queryMyComment(comment.getNovelId(), comment.getUserId(), comment.getReply());
        return comment1;

    }

    @RequestMapping("/queryNovelComment")
    public List<Comment> queryNovelComment(@RequestParam("novelId") Integer novelId){
        List<Comment> comments = commentMapper.queryNovelComment(novelId);
        comments.forEach(comment -> {
            User user = userMapper.getUser(comment.getUserId());
            comment.setUsername(user.getUsername());
            comment.setAvatar(user.getAvatar());
        });
        return comments;

    }

    //回复评论
    @RequestMapping("/replyComment")
    public int replyComment(@RequestBody Comment comment) {
        int insert = commentMapper.insert(comment);
        return insert;
    }

    @RequestMapping("/queryReplyComment")
    public List<Comment> queryReplyComment(@RequestParam("replyuser") Integer replyuser){
        List<Comment> comments = commentMapper.queryReplyComment(replyuser);
        comments.forEach(comment -> {
            User user = userMapper.getUser(comment.getUserId());
            comment.setUsername(user.getUsername());
            comment.setAvatar(user.getAvatar());
        });
        return comments;
    }



}
